package com.amit.Basic_Simple_Program;

import java.util.Scanner;

public class Binary_Search {

	public static void main(String[] args) {
		
		int n,first,last,middle,array[],search;
		
		System.out.println("Enter the number for arrays element");
		
		Scanner sc=new Scanner(System.in);
		 n=sc.nextInt();
		 array=new int[n];
		 
		 System.out.println("Enter the element of array in assending order");
		 
		 for(int c=0;c<n;c++)
		 {
			 array[c]=sc.nextInt();
		 }
		first=0;
		last=n-1;
		middle=(first+last)/2;
		
		System.out.println("Enter the element for search");
		
		search=sc.nextInt();
		
		while(first<=last)
		{
			if(array[middle]<search)
			
				first=middle+1;
			
			else if(array[middle]==search)
			{
			
				System.out.println(search+" "+"found at location"+" "+(middle+1));
				break;
			}
			else
			
				last=middle-1;
				middle=(first+last)/2;
			
		}
		
		if(first>last)
		{
			System.out.println(search+"is not present in list");
		}
		
		

	}

}
